﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using GeneralMotionControlProject.WPF;
using System.Windows;
using HostComputerPlatform.Services;
using ModuleBase.googoltech.GTS;
using ModuleBase;
using Newtonsoft.Json;
using NLog;
using Prism.Ioc;
using Prism.Regions;
using Prism.Commands;
using Prism.Services.Dialogs;
using System.Timers;
using HostComputerPlatform.Display;
using GeneralMotionControlProject.Common.Services;
using Microsoft.Extensions.Logging;
using NLog.Targets;
using System.Security.Cryptography;

namespace HostComputerPlatform.ViewModels
{
    public class SystemDialogViewModel : DialogViewModel
    {
        #region 变量
        private System.Timers.Timer aTimer;
        private readonly IAxisCardService axisCardService;
        private bool isBusy = false;
        private bool isJogButtonPressing = false, isPrtMoving = false;
        private CancellationTokenSource source0, source1;
        private DateTime lastJogClickDt = DateTime.Now;
        #endregion
        #region 属性
        private ObservableCollection<bool> eXI;
        public ObservableCollection<bool> EXI
        {
            get { return eXI; }
            set { SetProperty(ref eXI, value); }
        }
        private ObservableCollection<bool> eXO;
        public ObservableCollection<bool> EXO
        {
            get { return eXO; }
            set { SetProperty(ref eXO, value); }
        }
        private ObservableCollection<AxisStatusDisp> axisStatusDisps;
        public ObservableCollection<AxisStatusDisp> AxisStatusDisps
        {
            get { return axisStatusDisps; }
            set { SetProperty(ref axisStatusDisps, value); }
        }
        private bool _isBusy;
        public bool IsBusy
        {
            get { return _isBusy; }
            set { SetProperty(ref _isBusy, value); }
        }
        private double jogDist;
        public double JogDist
        {
            get { return jogDist; }
            set { SetProperty(ref jogDist, value); }
        }
        private double xJogSpeed;
        public double XJogSpeed
        {
            get { return xJogSpeed; }
            set { SetProperty(ref xJogSpeed, value); }
        }
        private double yJogSpeed;
        public double YJogSpeed
        {
            get { return yJogSpeed; }
            set { SetProperty(ref yJogSpeed, value); }
        }
        private double zJogSpeed;
        public double ZJogSpeed
        {
            get { return zJogSpeed; }
            set { SetProperty(ref zJogSpeed, value); }
        }
        private double rJogSpeed;
        public double RJogSpeed
        {
            get { return rJogSpeed; }
            set { SetProperty(ref rJogSpeed, value); }
        }
        private ObservableCollection<AxisNormalPointDisp> paramList1;
        public ObservableCollection<AxisNormalPointDisp> ParamList1
        {
            get { return paramList1; }
            set { SetProperty(ref paramList1, value); }
        }
        private ObservableCollection<AxisNormalPointDisp> paramList2;
        public ObservableCollection<AxisNormalPointDisp> ParamList2
        {
            get { return paramList2; }
            set { SetProperty(ref paramList2, value); }
        }
        #endregion
        #region 命令
        private DelegateCommand<object> iOOutCommand;
        public DelegateCommand<object> IOOutCommand =>
            iOOutCommand ?? (iOOutCommand = new DelegateCommand<object>(ExecuteIOOutCommand));
        private DelegateCommand<object> clearAlarmCommand;
        public DelegateCommand<object> ClearAlarmCommand =>
            clearAlarmCommand ?? (clearAlarmCommand = new DelegateCommand<object>(ExecuteClearAlarmCommand));
        private DelegateCommand<object> servoOnCommand;
        public DelegateCommand<object> ServoOnCommand =>
            servoOnCommand ?? (servoOnCommand = new DelegateCommand<object>(ExecuteServoOnCommand));
        private DelegateCommand<object> jog_MouseDownCommand;
        public DelegateCommand<object> Jog_MouseDownCommand =>
            jog_MouseDownCommand ?? (jog_MouseDownCommand = new DelegateCommand<object>(ExecuteJog_MouseDownCommand));
        private DelegateCommand jog_MouseUpCommand;
        public DelegateCommand Jog_MouseUpCommand =>
            jog_MouseUpCommand ?? (jog_MouseUpCommand = new DelegateCommand(ExecuteJog_MouseUpCommand));
        private DelegateCommand<object> teachNormalPointCommand;
        public DelegateCommand<object> TeachNormalPointCommand =>
            teachNormalPointCommand ?? (teachNormalPointCommand = new DelegateCommand<object>(ExecuteTeachNormalPointCommand));
        private DelegateCommand<object> teachNormalPoint2Command;
        public DelegateCommand<object> TeachNormalPoint2Command =>
            teachNormalPoint2Command ?? (teachNormalPoint2Command = new DelegateCommand<object>(ExecuteTeachNormalPoint2Command));
        private DelegateCommand openFileCommand;
        public DelegateCommand OpenFileCommand =>
            openFileCommand ?? (openFileCommand = new DelegateCommand(ExecuteOpenFileCommand));
        void ExecuteOpenFileCommand()
        {

        }
        void ExecuteTeachNormalPointCommand(object obj)
        {
            if (obj is AxisNormalPointDisp point)
            {
                if (!axisCardService.Connected) return;
                if (System.Windows.Forms.MessageBox.Show($"确认示教\"{point.Name}\"吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
                {
                    using var mdb = containerProvider.Resolve<MDbContext>();
                    switch (point.Name)
                    {
                        case "初始点":
                            {
                                var _pointParam = mdb.MParams.FirstOrDefault(param => param.Hand == 0 && param.Name == "PointInit");
                                if (_pointParam != null)
                                {
                                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                                    point.Y = _point.Y = Math.Round(GTSCard.GetEnc(axisCardService.A1), 3);
                                    point.Z = _point.Z = Math.Round(GTSCard.GetEnc(axisCardService.A2), 3);
                                    point.R = _point.R = Math.Round(GTSCard.GetEnc(axisCardService.A3), 3);
                                    _pointParam.Value = JsonConvert.SerializeObject(_point, Formatting.Indented);
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        case "拍膜位":
                            {
                                var _pointParam = mdb.MParams.FirstOrDefault(param => param.Hand == 0 && param.Name == "PhotoMoldPosition");
                                if (_pointParam != null)
                                {
                                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                                    point.Y = _point.Y = Math.Round(GTSCard.GetEnc(axisCardService.A1), 3);
                                    point.Z = _point.Z = Math.Round(GTSCard.GetEnc(axisCardService.A2), 3);
                                    point.R = _point.R = Math.Round(GTSCard.GetEnc(axisCardService.A3), 3);
                                    _pointParam.Value = JsonConvert.SerializeObject(_point, Formatting.Indented);
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        case "丢膜位":
                            {
                                var _pointParam = mdb.MParams.FirstOrDefault(param => param.Hand == 0 && param.Name == "DiscardPosZ");
                                if (_pointParam != null)
                                {
                                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                                    point.Y = _point.Y = Math.Round(GTSCard.GetEnc(axisCardService.A1), 3);
                                    point.Z = _point.Z = Math.Round(GTSCard.GetEnc(axisCardService.A2), 3);
                                    point.R = _point.R = Math.Round(GTSCard.GetEnc(axisCardService.A3), 3);
                                    _pointParam.Value = JsonConvert.SerializeObject(_point, Formatting.Indented);
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        case "贴膜位":
                            {
                                var _pointParam = mdb.MParams.FirstOrDefault(param => param.Hand == 0 && param.Name == "MoldPosition");
                                if (_pointParam != null)
                                {
                                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                                    point.Z = _point.Z = Math.Round(GTSCard.GetEnc(axisCardService.A2), 3);
                                    _pointParam.Value = JsonConvert.SerializeObject(_point, Formatting.Indented);
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        case "取膜位":
                            {
                                var _pointParam = mdb.MParams.FirstOrDefault(param => param.Hand == 0 && param.Name == "TakeMoldPosition");
                                if (_pointParam != null)
                                {
                                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                                    point.Y = _point.Y = Math.Round(GTSCard.GetEnc(axisCardService.A1), 3);
                                    point.Z = _point.Z = Math.Round(GTSCard.GetEnc(axisCardService.A2), 3);
                                    point.R = _point.R = Math.Round(GTSCard.GetEnc(axisCardService.A3), 3);
                                    _pointParam.Value = JsonConvert.SerializeObject(_point, Formatting.Indented);
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        void ExecuteTeachNormalPoint2Command(object obj)
        {
            if (obj is AxisNormalPointDisp point)
            {
                if (!axisCardService.Connected) return;
                if (System.Windows.Forms.MessageBox.Show($"确认示教\"{point.Name}\"吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
                {
                    using var mdb = containerProvider.Resolve<MDbContext>();
                    switch (point.Name)
                    {
                        case "初始点":
                            {
                                var _pointParam = mdb.MParams.FirstOrDefault(param => param.Hand == 1 && param.Name == "PointInit");
                                if (_pointParam != null)
                                {
                                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                                    point.Y = _point.Y = Math.Round(GTSCard.GetEnc(axisCardService.A4), 3);
                                    point.Z = _point.Z = Math.Round(GTSCard.GetEnc(axisCardService.A5), 3);
                                    point.R = _point.R = Math.Round(GTSCard.GetEnc(axisCardService.A6), 3);
                                    _pointParam.Value = JsonConvert.SerializeObject(_point, Formatting.Indented);
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        case "拍膜位":
                            {
                                var _pointParam = mdb.MParams.FirstOrDefault(param => param.Hand == 1 && param.Name == "PhotoMoldPosition");
                                if (_pointParam != null)
                                {
                                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                                    point.Y = _point.Y = Math.Round(GTSCard.GetEnc(axisCardService.A4), 3);
                                    point.Z = _point.Z = Math.Round(GTSCard.GetEnc(axisCardService.A5), 3);
                                    point.R = _point.R = Math.Round(GTSCard.GetEnc(axisCardService.A6), 3);
                                    _pointParam.Value = JsonConvert.SerializeObject(_point, Formatting.Indented);
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        case "丢膜位":
                            {
                                var _pointParam = mdb.MParams.FirstOrDefault(param => param.Hand == 1 && param.Name == "DiscardPosZ");
                                if (_pointParam != null)
                                {
                                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                                    point.Y = _point.Y = Math.Round(GTSCard.GetEnc(axisCardService.A4), 3);
                                    point.Z = _point.Z = Math.Round(GTSCard.GetEnc(axisCardService.A5), 3);
                                    point.R = _point.R = Math.Round(GTSCard.GetEnc(axisCardService.A6), 3);
                                    _pointParam.Value = JsonConvert.SerializeObject(_point, Formatting.Indented);
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        case "贴膜位":
                            {
                                var _pointParam = mdb.MParams.FirstOrDefault(param => param.Hand == 1 && param.Name == "MoldPosition");
                                if (_pointParam != null)
                                {
                                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                                    point.Z = _point.Z = Math.Round(GTSCard.GetEnc(axisCardService.A5), 3);
                                    _pointParam.Value = JsonConvert.SerializeObject(_point, Formatting.Indented);
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        case "取膜位":
                            {
                                var _pointParam = mdb.MParams.FirstOrDefault(param => param.Hand == 1 && param.Name == "TakeMoldPosition");
                                if (_pointParam != null)
                                {
                                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                                    point.Y = _point.Y = Math.Round(GTSCard.GetEnc(axisCardService.A4), 3);
                                    point.Z = _point.Z = Math.Round(GTSCard.GetEnc(axisCardService.A5), 3);
                                    point.R = _point.R = Math.Round(GTSCard.GetEnc(axisCardService.A6), 3);
                                    _pointParam.Value = JsonConvert.SerializeObject(_point, Formatting.Indented);
                                    mdb.SaveChanges();
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        void ExecuteJog_MouseUpCommand()
        {
            if (!axisCardService.Connected) return;
            isJogButtonPressing = false;
            if (!isPrtMoving)
            {
                GTSCard.AxisStop(axisCardService.A1, 0);
                GTSCard.AxisStop(axisCardService.A2, 0);
                GTSCard.AxisStop(axisCardService.A3, 0);
                source1?.Cancel();
                IsBusy = false;
            }
        }
        void ExecuteJog_MouseDownCommand(object obj)
        {
            if (!axisCardService.Connected) return;
            bool ismoving = false;
            foreach (var disp in AxisStatusDisps)
            {
                if (disp.MoveState)
                {
                    stopAllAxis();
                    ismoving = true;
                    break;
                }
            }
            if (ismoving || (DateTime.Now - lastJogClickDt).TotalSeconds < 0.2)
            {
                return;
            }
            int index = int.Parse(obj.ToString());
            int dir = index % 2 != 0 ? 1 : 0;
            isJogButtonPressing = true;
            if (!isPrtMoving && !IsBusy)
            {
                IsBusy = true;
                isPrtMoving = true;
                AxisParm _axis;
                double _speed;
                switch (index)
                {
                    case 1:
                    case 2:
                        _axis = axisCardService.A1;
                        _speed = YJogSpeed;
                        break;
                    case 3:
                    case 4:
                        _axis = axisCardService.A2;
                        _speed = ZJogSpeed;
                        break;
                    case 5:
                    case 6:
                        _axis = axisCardService.A3;
                        _speed = RJogSpeed;
                        break;
                    case 7:
                    case 8:
                        _axis = axisCardService.A4;
                        _speed = YJogSpeed;
                        break;
                    case 9:
                    case 10:
                        _axis = axisCardService.A5;
                        _speed = ZJogSpeed;
                        break;
                    case 11:
                    case 12:
                        _axis = axisCardService.A6;
                        _speed = RJogSpeed;
                        break;
                    case 13:
                    case 14:
                        _axis = axisCardService.A7;
                        _speed = XJogSpeed;
                        break;
                    default:
                        _axis = axisCardService.A1;
                        _speed = YJogSpeed;
                        break;
                }
                var curPos = GTSCard.GetPos(_axis);
                double _tarPos = 0;
                if (dir == 1)
                {
                    _tarPos = curPos + JogDist;
                }
                else
                {
                    _tarPos = curPos - JogDist;
                }
                source1 = new CancellationTokenSource();
                CancellationToken token = source1.Token;
                Task.Run(() => GoAction(token, _axis, _tarPos, _speed), token).ContinueWith(arg =>
                {
                    isPrtMoving = false;
                    if (isJogButtonPressing)
                    {
                        GTSCard.AxisJog(_axis, (ushort)dir, _speed);
                    }
                    else
                    {
                        IsBusy = false;
                    }
                });
            }
        }
        void ExecuteServoOnCommand(object obj)
        {
            if (!axisCardService.Connected) return;
            switch (obj.ToString())
            {
                case "0":
                    {
                        if (AxisStatusDisps[0].ServoOnState)
                        {
                            GTSCard.ServoOff(axisCardService.A1);
                        }
                        else
                        {
                            GTSCard.ServoOn(axisCardService.A1);
                            GTSCard.AxisPosSet(axisCardService.A1, GTSCard.GetEnc(axisCardService.A1));
                        }
                    }
                    break;
                case "1":
                    {
                        if (AxisStatusDisps[1].ServoOnState)
                        {
                            GTSCard.ServoOff(axisCardService.A2);
                        }
                        else
                        {
                            GTSCard.ServoOn(axisCardService.A2);
                            GTSCard.AxisPosSet(axisCardService.A2, GTSCard.GetEnc(axisCardService.A2));
                        }
                    }
                    break;
                case "2":
                    {
                        if (AxisStatusDisps[2].ServoOnState)
                        {
                            GTSCard.ServoOff(axisCardService.A3);
                        }
                        else
                        {
                            GTSCard.ServoOn(axisCardService.A3);
                            GTSCard.AxisPosSet(axisCardService.A3, GTSCard.GetEnc(axisCardService.A3));
                        }
                    }
                    break;
                case "3":
                    {
                        if (AxisStatusDisps[3].ServoOnState)
                        {
                            GTSCard.ServoOff(axisCardService.A4);
                        }
                        else
                        {
                            GTSCard.ServoOn(axisCardService.A4);
                            GTSCard.AxisPosSet(axisCardService.A4, GTSCard.GetEnc(axisCardService.A4));
                        }
                    }
                    break;
                case "4":
                    {
                        if (AxisStatusDisps[4].ServoOnState)
                        {
                            GTSCard.ServoOff(axisCardService.A5);
                        }
                        else
                        {
                            GTSCard.ServoOn(axisCardService.A5);
                            GTSCard.AxisPosSet(axisCardService.A5, GTSCard.GetEnc(axisCardService.A5));
                        }
                    }
                    break;
                case "5":
                    {
                        if (AxisStatusDisps[5].ServoOnState)
                        {
                            GTSCard.ServoOff(axisCardService.A6);
                        }
                        else
                        {
                            GTSCard.ServoOn(axisCardService.A6);
                            GTSCard.AxisPosSet(axisCardService.A6, GTSCard.GetEnc(axisCardService.A6));
                        }
                    }
                    break;
                case "6":
                    {
                        if (AxisStatusDisps[6].ServoOnState)
                        {
                            GTSCard.ServoOff(axisCardService.A7);
                        }
                        else
                        {
                            GTSCard.ServoOn(axisCardService.A7);
                            GTSCard.AxisPosSet(axisCardService.A7, GTSCard.GetEnc(axisCardService.A7));
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        void ExecuteClearAlarmCommand(object obj)
        {
            if (!axisCardService.Connected) return;
            if (!isBusy)
            {
                isBusy = true;
                Task.Run(() =>
                {
                    switch (obj.ToString())
                    {
                        case "0":
                            GTSCard.ClearAlm(axisCardService.A1);
                            break;
                        case "1":
                            GTSCard.ClearAlm(axisCardService.A2);
                            break;
                        case "2":
                            GTSCard.ClearAlm(axisCardService.A3);
                            break;
                        default:
                            break;
                    }
                }).ContinueWith(art =>
                {
                    isBusy = false;
                });
            }
        }
        void ExecuteIOOutCommand(object obj)
        {
            GTSCard.SetDo((short)axisCardService.A1.CardNo, (short)int.Parse(obj.ToString()), (short)(EXO[int.Parse(obj.ToString())] ? 1 : 0));
        }
        #endregion
        #region 导航
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            //进入
            aTimer.Enabled = true;
            if (axisCardService.Connected)
            {
                var doport = GTSCard.GetDoPort1((short)axisCardService.A1.CardNo);
                for (int i = 0; i < 16; i++)
                {
                    EXO[i] = (doport & 1 << i) == 0;
                }
            }
            LoadParamFromDb();
        }

        public override void OnDialogClosed()
        {
            aTimer.Enabled = false;
        }
        #endregion
        #region 构造函数
        public SystemDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            axisCardService = containerProvider.Resolve<IAxisCardService>("Top_A8");
            ParamList1 = new ObservableCollection<AxisNormalPointDisp>();
            ParamList2 = new ObservableCollection<AxisNormalPointDisp>();
            EXI = new ObservableCollection<bool>();
            for (int i = 0; i < 16; i++)
            {
                EXI.Add(false);
            }
            EXO = new ObservableCollection<bool>();
            for (int i = 0; i < 16; i++)
            {
                EXO.Add(false);
            }
            AxisStatusDisps = new ObservableCollection<AxisStatusDisp>();
            for (int i = 0; i < 8; i++)
            {
                AxisStatusDisps.Add(new AxisStatusDisp());
            }
            SetTimer();
        }
        #endregion
        #region 功能函数
        private void SetTimer()
        {
            // 创建一个每两秒触发一次事件的计时器
            aTimer = new System.Timers.Timer(500);
            // 钩住计时器的 Elapsed 事件
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
        }
        private void OnTimedEvent(object sender, ElapsedEventArgs e)
        {
            if (!axisCardService.Connected) return;
            //IO
            var diport = GTSCard.GetDiPort1((short)axisCardService.A1.CardNo);
            for (int i = 0; i < 16; i++)
            {
                EXI[i] = (diport & 1 << i) != 0;
            }

            try
            {
                AxisStatus astatus = GTSCard.GetAxisStatus(axisCardService.A1);
                AxisStatusDisps[0].AlmState = astatus.FlagAlm;
                AxisStatusDisps[0].HomeState = astatus.FlagHome;
                AxisStatusDisps[0].PosLimitState = astatus.FlagPosLimit;
                AxisStatusDisps[0].NeglimitState = astatus.FlagNeglimit;
                AxisStatusDisps[0].ServoOnState = astatus.FlagServoOn;
                AxisStatusDisps[0].MoveState = !astatus.FlagMoveEnd;
                AxisStatusDisps[0].PosPrf = Math.Round(astatus.PrfPos, 3);
                AxisStatusDisps[0].PosEnc = Math.Round(astatus.EncPos, 3);

                astatus = GTSCard.GetAxisStatus(axisCardService.A2);
                AxisStatusDisps[1].AlmState = astatus.FlagAlm;
                AxisStatusDisps[1].HomeState = astatus.FlagHome;
                AxisStatusDisps[1].PosLimitState = astatus.FlagPosLimit;
                AxisStatusDisps[1].NeglimitState = astatus.FlagNeglimit;
                AxisStatusDisps[1].ServoOnState = astatus.FlagServoOn;
                AxisStatusDisps[1].MoveState = !astatus.FlagMoveEnd;
                AxisStatusDisps[1].PosPrf = Math.Round(astatus.PrfPos, 3);
                AxisStatusDisps[1].PosEnc = Math.Round(astatus.EncPos, 3);

                astatus = GTSCard.GetAxisStatus(axisCardService.A3);
                AxisStatusDisps[2].AlmState = astatus.FlagAlm;
                AxisStatusDisps[2].HomeState = astatus.FlagHome;
                AxisStatusDisps[2].PosLimitState = astatus.FlagPosLimit;
                AxisStatusDisps[2].NeglimitState = astatus.FlagNeglimit;
                AxisStatusDisps[2].ServoOnState = astatus.FlagServoOn;
                AxisStatusDisps[2].MoveState = !astatus.FlagMoveEnd;
                AxisStatusDisps[2].PosPrf = Math.Round(astatus.PrfPos, 3);
                AxisStatusDisps[2].PosEnc = Math.Round(astatus.EncPos, 3);

                astatus = GTSCard.GetAxisStatus(axisCardService.A4);
                AxisStatusDisps[3].AlmState = astatus.FlagAlm;
                AxisStatusDisps[3].HomeState = astatus.FlagHome;
                AxisStatusDisps[3].PosLimitState = astatus.FlagPosLimit;
                AxisStatusDisps[3].NeglimitState = astatus.FlagNeglimit;
                AxisStatusDisps[3].ServoOnState = astatus.FlagServoOn;
                AxisStatusDisps[3].MoveState = !astatus.FlagMoveEnd;
                AxisStatusDisps[3].PosPrf = Math.Round(astatus.PrfPos, 3);
                AxisStatusDisps[3].PosEnc = Math.Round(astatus.EncPos, 3);

                astatus = GTSCard.GetAxisStatus(axisCardService.A5);
                AxisStatusDisps[4].AlmState = astatus.FlagAlm;
                AxisStatusDisps[4].HomeState = astatus.FlagHome;
                AxisStatusDisps[4].PosLimitState = astatus.FlagPosLimit;
                AxisStatusDisps[4].NeglimitState = astatus.FlagNeglimit;
                AxisStatusDisps[4].ServoOnState = astatus.FlagServoOn;
                AxisStatusDisps[4].MoveState = !astatus.FlagMoveEnd;
                AxisStatusDisps[4].PosPrf = Math.Round(astatus.PrfPos, 3);
                AxisStatusDisps[4].PosEnc = Math.Round(astatus.EncPos, 3);

                astatus = GTSCard.GetAxisStatus(axisCardService.A6);
                AxisStatusDisps[5].AlmState = astatus.FlagAlm;
                AxisStatusDisps[5].HomeState = astatus.FlagHome;
                AxisStatusDisps[5].PosLimitState = astatus.FlagPosLimit;
                AxisStatusDisps[5].NeglimitState = astatus.FlagNeglimit;
                AxisStatusDisps[5].ServoOnState = astatus.FlagServoOn;
                AxisStatusDisps[5].MoveState = !astatus.FlagMoveEnd;
                AxisStatusDisps[5].PosPrf = Math.Round(astatus.PrfPos, 3);
                AxisStatusDisps[5].PosEnc = Math.Round(astatus.EncPos, 3);

                astatus = GTSCard.GetAxisStatus(axisCardService.A7);
                AxisStatusDisps[6].AlmState = astatus.FlagAlm;
                AxisStatusDisps[6].HomeState = astatus.FlagHome;
                AxisStatusDisps[6].PosLimitState = astatus.FlagPosLimit;
                AxisStatusDisps[6].NeglimitState = astatus.FlagNeglimit;
                AxisStatusDisps[6].ServoOnState = astatus.FlagServoOn;
                AxisStatusDisps[6].MoveState = !astatus.FlagMoveEnd;
                AxisStatusDisps[6].PosPrf = Math.Round(astatus.PrfPos, 3);
                AxisStatusDisps[6].PosEnc = Math.Round(astatus.EncPos, 3);
            }
            catch (Exception ex)
            {

            }
        }
        private void GoAction(CancellationToken token, AxisParm axisN, double tartgetN, double speed)
        {
            try
            {
                int stepnum = 0;
                Stopwatch sw = Stopwatch.StartNew();
                while (true)
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    switch (stepnum)
                    {
                        case 0:
                            {
                                GTSCard.AxisPosMove(ref axisN, tartgetN, speed);
                                stepnum = 1;
                            }
                            break;
                        case 1:
                            if (GTSCard.AxisPosMoveCheckDone(axisN))
                            {
                                stepnum = 2;
                            }
                            break;
                        case 2:
                            if (sw.Elapsed.TotalSeconds > 0.5)
                            {
                                sw.Stop();
                                return;
                            }
                            break;
                        default:
                            break;
                    }
                    System.Threading.Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
            }
        }
        private void stopAllAxis()
        {
            GTSCard.AxisStop(axisCardService.A1, 0);
            GTSCard.AxisStop(axisCardService.A2, 0);
            GTSCard.AxisStop(axisCardService.A3, 0);
            GTSCard.AxisStop(axisCardService.A4, 0);
            GTSCard.AxisStop(axisCardService.A5, 0);
            GTSCard.AxisStop(axisCardService.A6, 0);
            GTSCard.AxisStop(axisCardService.A7, 0);
            if (source1 != null)
            {
                source1.Cancel();
            }
            IsBusy = false;
            isJogButtonPressing = false;
            isPrtMoving = false;
            lastJogClickDt = DateTime.Now;
        }
        private void LoadParamFromDb()
        {
            try
            {
                using var mdb = containerProvider.Resolve<MDbContext>();
                ParamList1.Clear();
                ParamList2.Clear();
                var _pointParam = mdb.MParams.FirstOrDefault(m => m.Name == "PointInit" && m.Hand == 0);
                if (_pointParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                    ParamList1.Add(new AxisNormalPointDisp
                    {
                        X = _point.X,
                        Y = _point.Y,
                        Z = _point.Z,
                        Name = "初始点",
                        Unit = "mm"
                    });
                }
                else
                {
                    ParamList1.Add(new AxisNormalPointDisp
                    {
                        X = 0,
                        Y = 0,
                        Z = 0,
                        Name = "初始点",
                        Unit = "mm"
                    });
                }
                _pointParam = mdb.MParams.FirstOrDefault(m => m.Name == "PointInit" && m.Hand == 1);
                if (_pointParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                    ParamList2.Add(new AxisNormalPointDisp
                    {
                        Y = _point.Y,
                        Z = _point.Z,
                        R = _point.R,
                        Name = "初始点",
                        Unit = "mm"
                    });
                }
                else
                {
                    ParamList2.Add(new AxisNormalPointDisp
                    {
                        X = 0,
                        Y = 0,
                        Z = 0,
                        Name = "初始点",
                        Unit = "mm"
                    });
                }
                _pointParam = mdb.MParams.FirstOrDefault(m => m.Name == "PhotoMoldPosition" && m.Hand == 0);
                if (_pointParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                    ParamList1.Add(new AxisNormalPointDisp
                    {
                        X = _point.X,
                        Y = _point.Y,
                        Z = _point.Z,
                        Name = "拍膜位",
                        Unit = "mm"
                    });
                }
                else
                {
                    ParamList1.Add(new AxisNormalPointDisp
                    {
                        X = 0,
                        Y = 0,
                        Z = 0,
                        Name = "拍膜位",
                        Unit = "mm"
                    });
                }
                _pointParam = mdb.MParams.FirstOrDefault(m => m.Name == "PhotoMoldPosition" && m.Hand == 1);
                if (_pointParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                    ParamList2.Add(new AxisNormalPointDisp
                    {
                        Y = _point.Y,
                        Z = _point.Z,
                        R = _point.R,
                        Name = "拍膜位",
                        Unit = "mm"
                    });
                }
                else
                {
                    ParamList2.Add(new AxisNormalPointDisp
                    {
                        X = 0,
                        Y = 0,
                        Z = 0,
                        Name = "拍膜位",
                        Unit = "mm"
                    });
                }
                _pointParam = mdb.MParams.FirstOrDefault(m => m.Name == "DiscardPosZ" && m.Hand == 0);
                if (_pointParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                    ParamList1.Add(new AxisNormalPointDisp
                    {
                        X = _point.X,
                        Y = _point.Y,
                        Z = _point.Z,
                        Name = "丢膜位",
                        Unit = "mm"
                    });
                }
                else
                {
                    ParamList1.Add(new AxisNormalPointDisp
                    {
                        X = 0,
                        Y = 0,
                        Z = 0,
                        Name = "丢膜位",
                        Unit = "mm"
                    });
                }
                _pointParam = mdb.MParams.FirstOrDefault(m => m.Name == "DiscardPosZ" && m.Hand == 1);
                if (_pointParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                    ParamList2.Add(new AxisNormalPointDisp
                    {
                        Y = _point.Y,
                        Z = _point.Z,
                        R = _point.R,
                        Name = "丢膜位",
                        Unit = "mm"
                    });
                }
                else
                {
                    ParamList2.Add(new AxisNormalPointDisp
                    {
                        X = 0,
                        Y = 0,
                        Z = 0,
                        Name = "丢膜位",
                        Unit = "mm"
                    });
                }
                _pointParam = mdb.MParams.FirstOrDefault(m => m.Name == "MoldPosition" && m.Hand == 0);
                if (_pointParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                    ParamList1.Add(new AxisNormalPointDisp
                    {
                        X = _point.X,
                        Y = _point.Y,
                        Z = _point.Z,
                        Name = "贴膜位",
                        Unit = "mm"
                    });
                }
                else
                {
                    ParamList1.Add(new AxisNormalPointDisp
                    {
                        X = 0,
                        Y = 0,
                        Z = 0,
                        Name = "贴膜位",
                        Unit = "mm"
                    });
                }
                _pointParam = mdb.MParams.FirstOrDefault(m => m.Name == "MoldPosition" && m.Hand == 1);
                if (_pointParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                    ParamList2.Add(new AxisNormalPointDisp
                    {
                        Y = _point.Y,
                        Z = _point.Z,
                        R = _point.R,
                        Name = "贴膜位",
                        Unit = "mm"
                    });
                }
                else
                {
                    ParamList2.Add(new AxisNormalPointDisp
                    {
                        X = 0,
                        Y = 0,
                        Z = 0,
                        Name = "贴膜位",
                        Unit = "mm"
                    });
                }
                _pointParam = mdb.MParams.FirstOrDefault(m => m.Name == "TakeMoldPosition" && m.Hand == 0);
                if (_pointParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                    ParamList1.Add(new AxisNormalPointDisp
                    {
                        X = _point.X,
                        Y = _point.Y,
                        Z = _point.Z,
                        Name = "取膜位",
                        Unit = "mm"
                    });
                }
                else
                {
                    ParamList1.Add(new AxisNormalPointDisp
                    {
                        X = 0,
                        Y = 0,
                        Z = 0,
                        Name = "取膜位",
                        Unit = "mm"
                    });
                }
                _pointParam = mdb.MParams.FirstOrDefault(m => m.Name == "TakeMoldPosition" && m.Hand == 1);
                if (_pointParam != null)
                {
                    var _point = JsonConvert.DeserializeObject<MPoint>(_pointParam.Value);
                    ParamList2.Add(new AxisNormalPointDisp
                    {
                        Y = _point.Y,
                        Z = _point.Z,
                        R = _point.R,
                        Name = "取膜位",
                        Unit = "mm"
                    });
                }
                else
                {
                    ParamList2.Add(new AxisNormalPointDisp
                    {
                        X = 0,
                        Y = 0,
                        Z = 0,
                        Name = "取膜位",
                        Unit = "mm"
                    });
                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion
    }
}
